define(['store'], function (store) {
    'use strict';
    function ctrl($scope,LocalCacheService, $state, $ionicLoading, $timeout, CommonService, DeviceService, IdentificationService) {
        function init() {
            $scope.identification = {
                photo: {
                    positive: '../img/id_positive.png',
                    negative: '../img/id_negative.png'
                },
                status: true
            };
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
        $scope.getPhoto = function (type) {
            DeviceService.showSelectDevice(function (data) {
                if (data.result && data.result.base64) {
                    // 设置图片
                    $timeout(function () {
                        type == 'positive' ? $scope.identification.photo.positive = data.result.base64 : $scope.identification.photo.negative = data.result.base64;
                        if ($scope.identification.photo.positive.indexOf('.png') < 0 && $scope.identification.photo.negative.indexOf('.png') < 0) {
                            $scope.identification.status = false;
                        }
                    }, 1000);
                }
            });
        };
        $scope.submit = function () {
            rspWeb($scope.identification.photo.positive.substring(22), '6A', function () {
                rspWeb($scope.identification.photo.negative.substring(22), '6B', null)
            });
        };

        function rspWeb(imgData, type, callback) {
            $ionicLoading.show();
            var userInfo = LocalCacheService.getUser();
            IdentificationService.getCheckPhotoInfo({
                image_no: type,
                image_data: imgData,
                mobile_tel:userInfo.mobile_tel
            }).then(function (data) {
                    $ionicLoading.hide();
                    if (data.error_no != 0) {
                        CommonService.showAlert({message: data.error_info});
                        return;
                    }
                    if (type == '6A') {
                        //store.set('idCard1', {addr: data.id_address});
                        callback();
                    }
                    if (type == '6B') {
                        //store.set('idCard2', {
                        //    policeorg: data.issued_depart,
                        //    idbegindate: data.id_begindate,
                        //    idenddate: data.id_enddate
                        //});
                        var params = {
                            id_begindate: data.id_begindate,
                            id_enddate: data.id_enddate,
                            business_info:store.get('idCardCheck').mobile_tel,
                            status:'2'
                        };
                        IdentificationService.uploadCheckSumbit(params)
                            .then(function (data) {
                                    $ionicLoading.hide();
                                    if (data.error_no != 0) {
                                        CommonService.showAlert({message: data.error_info});
                                        return;
                                    }
                                    $state.go('tab.account-resetPhoneThird');
                                }, function(result){
                                    $ionicLoading.hide();
                                    CommonService.showAlert({message: result.error_info});
                                }
                            );
                    }
                }, function (result) {
                    $ionicLoading.hide();
                    CommonService.showAlert({message: result.error_info});
                }
            );
        }
    }

    ctrl.$inject = ['$scope', 'LocalCacheService', '$state', '$ionicLoading', '$timeout', 'CommonService', 'DeviceService', 'IdentificationService'];
    return ctrl;
});